package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.base.SearchPara;
import com.linsir.core.BaseResponse;
import com.linsir.core.CodeEnum;
import com.linsir.lin.dto.GridDTO;
import com.linsir.lin.mapper.BaseCodeMapper;
import com.linsir.lin.pojo.BaseCode;
import com.linsir.lin.service.BaseCodeService;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class BaseCodeServiceImpl  extends ServiceImpl<BaseCodeMapper, BaseCode> implements IService<BaseCode>, BaseCodeService {
    @Override
    public BaseResponse<Boolean> add(BaseCode baseCode) {
        Boolean data =save(baseCode);
        if (data)
        {
        return new BaseResponse<Boolean>(CodeEnum.SUCCESS,"",data);
        }
        else
        {
            return new BaseResponse<Boolean>(CodeEnum.FAILED,"",data);
        }
    }

    /**
     * @param page
     * @param limit
     * @param searchParams
     * @return
     */
    @Override
    public GridDTO<BaseCode> grid(String page, String limit, String searchParams) throws IOException {
        Map<String,String> map = SearchPara.toMap(searchParams);

        QueryWrapper<BaseCode> wrapper=new QueryWrapper<BaseCode>();

        if (map!=null)
        {
            wrapper.eq("name",map.get("name")).or().eq("type_name",map.get("typeName"));
        }
        Page<BaseCode> pageDto = new PageDto<BaseCode>();
        pageDto.setCurrent(Long.parseLong(page));
        pageDto.setSize(Long.parseLong(limit));
        pageDto = super.baseMapper.selectPage(pageDto,wrapper);
        return new GridDTO<BaseCode>("0","成功",pageDto.getTotal(),pageDto.getRecords());
    }





    @Override
    public List<BaseCode> list(String typeCode) {
        QueryWrapper<BaseCode> wrapper  = new  QueryWrapper<BaseCode>();
        wrapper.eq("type_code",typeCode);
        List<BaseCode> baseCodeList = super.baseMapper.selectList(wrapper);
        return baseCodeList;
    }

    /**
     * @param typeCode
     * @param code
     * @return
     */
    @Override
    public String codeName(String typeCode, String code) {
        QueryWrapper<BaseCode> wrapper  = new  QueryWrapper<BaseCode>();
        String codeStr=null;
        if (code.equals("true"))
        {
            codeStr="1";
        }
        else  if (code.equals("false"))
        {
            codeStr="0";
        }
        else {
            codeStr=code;
        }

        wrapper.eq("type_code",typeCode).eq("code",codeStr);
        BaseCode baseCode=getOne(wrapper);
        return baseCode.getName();
    }
}
